package Eyjafjalla.screens;

import Eyjafjalla.character.Eyjafjalla;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.modcore.ClassEnum;
import Eyjafjalla.modcore.EyjafjallaMod;
import basemod.BaseMod;
import basemod.abstracts.CustomSavable;
import basemod.interfaces.ISubscriber;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.esotericsoftware.spine.*;
import com.evacipated.cardcrawl.modthespire.lib.SpireConfig;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.io.IOException;
import java.util.ArrayList;

public class SkinSelectScreen implements ISubscriber, CustomSavable<Integer> {
    public static SkinSelectScreen Inst;
    public Hitbox leftHb;
    public Hitbox rightHb;
    public TextureAtlas atlas;
    public Skeleton skeleton;
    public AnimationStateData stateData;
    public AnimationState state;
    public String curName = "";
    public String nextName = "";
    public int index = 0;
    private static final ArrayList<Skin> skins;
    private static final UIStrings uiStrings;

    public static Skin getSkin(){
        if(Inst==null)
            return skins.get(0);
        return skins.get(Inst.index);
    }

    public void Initialize() {
        int i = EyjafjallaMod.DefaultSkinIndex;
        if (index != i && i >= 0) {
            index = i;
        }
        refresh();
    }

    public SkinSelectScreen(){
        this.index = 0;
        this.leftHb = new Hitbox(70.0F * Settings.scale, 70.0F * Settings.scale);
        this.rightHb = new Hitbox(70.0F * Settings.scale, 70.0F * Settings.scale);
        BaseMod.subscribe(this);
        BaseMod.addSaveField("eyjafjalla_skin", this);
        Initialize();
    }

    public void loadAnimation(int type,float scale){
        String pathName = "EyjafjallaResources/img/char/";
        String fileName = "";
        if(type==0){
            fileName = "yb/char_1016_agoat2";
        }
        else if(type==1){
            fileName = "ybcake/char_1016_agoat2_epoque_34";
        }
        else if(type==2){
            fileName = "ss/char_180_amgoat";
        }
        else if(type==3){
            fileName = "sssummer/char_180_amgoat_summer_5";
        }
        this.atlas = new TextureAtlas(Gdx.files.internal(pathName+fileName+".atlas"));
        SkeletonJson json = new SkeletonJson(this.atlas);
        json.setScale(Settings.renderScale / scale);
        SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal(pathName+fileName+".json"));
        this.skeleton = new Skeleton(skeletonData);
        this.skeleton.setColor(Color.WHITE);
        this.stateData = new AnimationStateData(skeletonData);
        this.state = new AnimationState(this.stateData);
        if(type==0)
            this.state.setAnimation(0, "Skill_1_Idle", true);
        else if(type==1){
            this.state.setAnimation(0, "Skill_1_Idle", true);
        }
        else if(type==2||type==3) {
            this.state.setAnimation(0, "Idle", true);
        }
    }

    public void refresh(){
        Skin skin = skins.get(index);
        this.curName = skin.name;
        this.loadAnimation(skin.index,1.75F);
        this.nextName = skins.get(nextIndex()).name;
        if(AbstractDungeon.player instanceof Eyjafjalla){
            Eyjafjalla eyjafjalla = (Eyjafjalla)AbstractDungeon.player;
            eyjafjalla.refreshSkin();
        }
    }

    public int prevIndex() {
        return this.index - 1 < 0 ? skins.size() - 1 : this.index - 1;
    }

    public int nextIndex() {
        return this.index + 1 > skins.size() - 1 ? 0 : this.index + 1;
    }

    public void update() {
        float centerX = (float)Settings.WIDTH * 0.2F;
        float centerY = (float)Settings.HEIGHT * 0.73F;
        this.leftHb.move(centerX - 200.0F * Settings.scale, centerY);
        this.rightHb.move(centerX + 200.0F * Settings.scale, centerY);
        this.updateInput();
    }

    private void updateInput() {
        if (CardCrawlGame.chosenCharacter == ClassEnum.Eyjafjalla_CLASS) {
            this.leftHb.update();
            this.rightHb.update();
            if (this.leftHb.clicked) {
                this.leftHb.clicked = false;
                CardCrawlGame.sound.play("UI_CLICK_1");
                this.index = this.prevIndex();
                try {
                    SpireConfig config = new SpireConfig("Eyjafjalla_FimmlpS","Common");
                    config.setInt("defaultSkin",this.index);
                    config.save();
                }catch (IOException var2){
                    var2.printStackTrace();
                }
                this.refresh();
            }

            if (this.rightHb.clicked) {
                this.rightHb.clicked = false;
                CardCrawlGame.sound.play("UI_CLICK_1");
                this.index = this.nextIndex();
                try {
                    SpireConfig config = new SpireConfig("Eyjafjalla_FimmlpS","Common");
                    config.setInt("defaultSkin",this.index);
                    config.save();
                }catch (IOException var2){
                    var2.printStackTrace();
                }
                this.refresh();
            }

            if (InputHelper.justClickedLeft) {
                if (this.leftHb.hovered) {
                    this.leftHb.clickStarted = true;
                }

                if (this.rightHb.hovered) {
                    this.rightHb.clickStarted = true;
                }
            }
        }

    }

    public void render(SpriteBatch sb) {
        float centerX = (float)Settings.WIDTH * 0.2F;
        float centerY = (float)Settings.HEIGHT * 0.73F;
        this.renderSkin(sb, centerX, centerY);
        FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, uiStrings.TEXT[0], centerX, centerY + 250.0F * Settings.scale, Color.WHITE, 1.25F);
        Color color = Settings.GOLD_COLOR.cpy();
        color.a /= 2.0F;
        float dist = 100.0F * Settings.scale;
        FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, this.curName, centerX, centerY, Settings.GOLD_COLOR);
        FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, this.nextName, centerX + dist * 1.5F, centerY - dist *0.5F, color);
        if (this.leftHb.hovered) {
            sb.setColor(Color.LIGHT_GRAY);
        } else {
            sb.setColor(Color.WHITE);
        }

        sb.draw(ImageMaster.CF_LEFT_ARROW, this.leftHb.cX - 24.0F, this.leftHb.cY - 24.0F, 24.0F, 24.0F, 48.0F, 48.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 48, 48, false, false);
        if (this.rightHb.hovered) {
            sb.setColor(Color.LIGHT_GRAY);
        } else {
            sb.setColor(Color.WHITE);
        }

        sb.draw(ImageMaster.CF_RIGHT_ARROW, this.rightHb.cX - 24.0F, this.rightHb.cY - 24.0F, 24.0F, 24.0F, 48.0F, 48.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 48, 48, false, false);
        this.rightHb.render(sb);
        this.leftHb.render(sb);
    }

    public void renderSkin(SpriteBatch sb, float x, float y) {
        if (this.atlas != null) {
            this.state.update(Gdx.graphics.getDeltaTime());
            this.state.apply(this.skeleton);
            this.skeleton.updateWorldTransform();
            this.skeleton.setPosition(x, y);
            sb.end();
            CardCrawlGame.psb.begin();
            AbstractCreature.sr.draw(CardCrawlGame.psb, this.skeleton);
            CardCrawlGame.psb.end();
            sb.begin();
        }
    }

    public static class Skin{
        public int index;
        public String name;

        public Skin(int index){
            this.index = index;
            name = uiStrings.TEXT[index+1];
        }
    }

    public void onLoad(Integer arg0) {
        this.index = arg0;
        this.refresh();
    }

    public Integer onSave() {
        return this.index;
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.SKIN_SELECT);
        skins = new ArrayList<>();
        skins.add(new Skin(0));
        skins.add(new Skin(1));
        skins.add(new Skin(2));
        skins.add(new Skin(3));
        Inst = new SkinSelectScreen();
    }
}


